package com.subway.babynote;
/*水*/
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.TimeZone;
import android.util.Log;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.database.Cursor;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.Date;

import com.subway.babynote.syncdata.StringUtil;
import com.subway.babynote.syncservice.syncapi;

public class perdaystatistics extends Activity {
	private static final int INVISIBLE = 0x00000004;
	private static final int VISIBLE = 0x00000001;
	private int my_Year;
	private int my_Month;
	private int my_Day;
	TextView total, date_view, userage_view;
	int today,before,after;
	String[] from;
	int[] to;
	ListView lv,total_lv;
	SimpleAdapter mAdapter;
	Cursor cur,total_cur;
	NotedbHelper notedb;
	Button login_button;
	Button previous_button;
	Button next_button;
	Intent In;
	Date date;
	String day;
	boolean expanding;
	private LinearLayout linearLayout, statistics, sync, more;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.perdaystatistics);
		setTitle(R.string.statisticsperday);
		In = new Intent();
	//	initTabFunction();
		notedb = new NotedbHelper(this);
		lv = (ListView) findViewById(R.id.listview);
		total_lv = (ListView) findViewById(R.id.totallist);
		login_button = (Button) findViewById(R.id.login_button);
		previous_button = (Button) findViewById(R.id.previousday_button);
		next_button = (Button) findViewById(R.id.nextday_button);
		userage_view = (TextView) findViewById(R.id.userinfo_TextView);
		total = (TextView) findViewById(R.id.totalitem);
		date_view =(TextView) findViewById(R.id.date_view);
		expanding=false;
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		my_Year = c.get(Calendar.YEAR);
		my_Month = c.get(Calendar.MONTH) ;
		my_Day = c.get(Calendar.DAY_OF_MONTH);
		// today = mYear + "-" + mMonth + "-" + mDay;
		date = new GregorianCalendar(my_Year, my_Month, my_Day, 0, 0).getTime();
		today = (int) (date.getTime() / 1000);
		setInfoFromDB(today);
	   // setSumFromDB(today);
		try {
			int age = notedb.getUserAge();
			if(age==-1){
				In.setClass(perdaystatistics.this, birth.class);
				startActivity(In);
			}
			int gender = notedb.getUserGender();
			if (gender == 0) {
				userage_view.setText( getResources().getString(R.string.babygirl)+ age +getResources().getString(R.string.monthold) );
			} else {
				userage_view.setText(getResources().getString(R.string.babyboy) + age + getResources().getString(R.string.monthold));
			}
			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
			setButton(today);
		} catch (Exception e) {
			Log.v("perdaystatistics", "from DB e=" + e.getMessage());
		}
		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				In.setClass(perdaystatistics.this, login.class);
				startActivity(In);
			}
		});

		previous_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			//	setContentView(R.layout.perdaystatistics);
				my_Day--;

				// previous=mYear + "-" + mMonth + "-" + mDay ;
				date = new GregorianCalendar(my_Year, my_Month, my_Day, 0, 0)
						.getTime();
			    before = (int) (date.getTime() / 1000);
				
				setButton(before);
				Log.v("perday", "before" + before);
				setInfoFromDB(before);
				v.invalidate();
			}
		});
		next_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			//	setContentView(R.layout.perdaystatistics);
				my_Day++;

				// previous=mYear + "-" + mMonth + "-" + mDay ;
				date = new GregorianCalendar(my_Year, my_Month, my_Day, 0, 0)
						.getTime();
				after = (int) (date.getTime() / 1000);
				Log.v("perday", "after" + after);
				setButton(after);		
				setInfoFromDB(after);
				v.invalidate();
				
			}
		});
		
	     total.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!expanding){
					expanding=true;
				}else{
					expanding=false;
				}
				date = new GregorianCalendar(my_Year, my_Month, my_Day, 0, 0)
				.getTime();
		         today = (int) (date.getTime() / 1000);
		         setSumFromDB(today);
		         v.invalidate();
			}
	     });
	}

	public void setButton(int day) {
		int previous = day - 3600 * 24;
		int next = day + 3600 * 24;
		previous_button.setVisibility(INVISIBLE);
		previous_button.setClickable(false);
		next_button.setVisibility(INVISIBLE);
		next_button.setClickable(false);
		try {
			Log.v("perday", "previous" + previous);
		
			if (syncapi.getSyncImpl().getFeeds(previous)!=null) {
				previous_button.setVisibility(VISIBLE);
				previous_button.setClickable(true);
			}
			
			Log.v("perday", "next" + next);
			
			if (syncapi.getSyncImpl().getFeeds(next)!=null) {
				next_button.setVisibility(VISIBLE);
				next_button.setClickable(true);
			}
		} catch (Exception e) {
			Log.v("SETBUTTON", "from DB e=" + e.getMessage());
		}
	}
	public void setSumFromDB(int date){
		try {
			total_cur = notedb.getSumFeeds(date);

			Log.v("perday", "feed count" + total_cur.getCount());
			from = new String[] { "name", "sum", "unit" };
			to = new int[] {  R.id.item1, R.id.item2, R.id.item3, };
			ArrayList<HashMap<String, Object>> feeds = new ArrayList<HashMap<String, Object>>();
			while (!total_cur.isAfterLast()) {
				HashMap<String, Object> feed = new HashMap<String, Object>();
				feed.put("name", total_cur.getString(0));
				feed.put("sum", total_cur.getFloat(1));
				feed.put("unit", total_cur.getString(2));
				feeds.add(feed);
				total_cur.moveToNext();
			}
			total_cur.close();
			if(!expanding){
	            	feeds.clear();
	        }
			mAdapter = new SimpleAdapter(this, feeds, R.layout.total_item, from,
					to);
			total_lv.setAdapter(mAdapter);
           
			
		} catch (Exception e) {
			total_cur.close();
			Log.v("perday", "set sum From DB e=" + e.getMessage());

		}
	}

	public void setInfoFromDB(int date) {
		try {
			cur = notedb.getFeeds(date);
			day=StringUtil.onlyday(StringUtil.int2date(date)).toString();
			date_view.setText(day);
			Log.v("perday", "feed count" + cur.getCount());
			from = new String[] { "time", "name", "quantity", "unit" };
			to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4, };
			ArrayList<HashMap<String, Object>> feeds = new ArrayList<HashMap<String, Object>>();
			while (!cur.isAfterLast()) {
				HashMap<String, Object> feed = new HashMap<String, Object>();
				feed.put("time", StringUtil.sql_date2str(StringUtil
						.int2date(cur.getInt(1))).substring(10,16));
				feed.put("name", cur.getString(2));
				feed.put("quantity", cur.getFloat(3));
				feed.put("unit", cur.getString(4));
				feeds.add(feed);
				cur.moveToNext();
			}
			cur.close();
			//lv.invalidateViews();
            
			mAdapter = new SimpleAdapter(this, feeds, R.layout.grid_item, from,
					to);
			lv.setAdapter(mAdapter);

			// getBillsTotal
			
			//Log.v("perday", notedb.getSumFeeds(date));
		//	total.setText(notedb.getSumFeeds(date));
		} catch (Exception e) {
			cur.close();
			Log.v("perday", "set Info From DB e=" + e.getMessage());

		}
	}

	

	public void onDestroy() {
		notedb.close();
		super.onDestroy();
	}
}